<?php

include DIRETORIO."app/Models/ProjectDAO.php";

class C_Project{

	protected static $lk = NULL;
	
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}
	
	public static function editProject($post){
		$return = null;

		$objProjectDAO	= new ProjectDAO(self::getDB());
		$rsProject		= $objProjectDAO->getProjects((int)$post[id]);

		$objUserGroupDAO = new UserGroupDAO(self::getDB());		
		$objUsersGroup1	 = $objUserGroupDAO->getUsersGroup(0,1);
		$objUsersGroup2	 = $objUserGroupDAO->getUsersGroup(0,2);
		
		$selectUsersGroup1 = "";
		foreach($objUsersGroup1 as $row){
			$selected = $rsProject[0]->idProductOwner == $row->idUser?" selected":"";
			$selectUsersGroup1 .= '<option value="'.$row->idUser.'"'.$selected.'>'.$row->name.'</option>';
		}

		$selectUsersGroup2 = "";
		foreach($objUsersGroup2 as $row){
			$selected = $rsProject[0]->idScrumMaster == $row->idUser?" selected":"";
			$selectUsersGroup2 .= '<option value="'.$row->idUser.'"'.$selected.'>'.$row->name.'</option>';
		}

		$html = file_get_contents(TEMPLATES."project.html");
		$html = str_replace("[TITLE]","Editar Projeto",$html);
		$html = str_replace("[ID_PROJECT]",$rsProject[0]->id,$html);
		$html = str_replace("[NAME_PROJECT]",$rsProject[0]->name,$html);
		$html = str_replace("[CUSTOMER]",$rsProject[0]->customer,$html);
		$html = str_replace("[URL_CALENDAR_GOOGLE]",$rsProject[0]->urlCalendarGoogle,$html);
		$html = str_replace("[LIST_PRODUCT_OWER]",$selectUsersGroup1,$html);
		$html = str_replace("[LIST_SCRUM_MASTER]",$selectUsersGroup2,$html);
		$html = str_replace("[DATE_BEGIN]",$rsProject[0]->dateBegin,$html);
		$html = str_replace("[DATE_END_ESTIMED]",$rsProject[0]->dateEndEstimated,$html);
		$html = str_replace("[DESCRIPTION]",$rsProject[0]->description,$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível buscar o Projeto!");
		}

		return json_encode($return);
	}

	public static function viewProject($post){
		$return = null;

		$objProjectDAO	= new ProjectDAO(self::getDB());
		$rsProject		= $objProjectDAO->getProjects((int)$post[id],0,array(1,2,3));

		$objUserGroupDAO = new UserGroupDAO(self::getDB());		
		$objUsersGroup1	 = $objUserGroupDAO->getUsersGroup(0,1);
		$objUsersGroup2	 = $objUserGroupDAO->getUsersGroup(0,2);
		
		$selectUsersGroup1 = "";
		foreach($objUsersGroup1 as $row){
			if($rsProject[0]->idProductOwner == $row->idUser){
				$selectUsersGroup1 = '<option value="'.$row->idUser.'"'.$selected.'>'.$row->name.'</option>';
				break;
			}
		}

		$selectUsersGroup2 = "";
		foreach($objUsersGroup2 as $row){
			if($rsProject[0]->idScrumMaster == $row->idUser){
				$selectUsersGroup2 = '<option value="'.$row->idUser.'"'.$selected.'>'.$row->name.'</option>';
				break;
			}
		}

		$html = file_get_contents(TEMPLATES."viewProject.html");
		$html = str_replace("[TITLE]","Detalhes do Projeto",$html);
		$html = str_replace("[NAME_PROJECT]",$rsProject[0]->name,$html);
		$html = str_replace("[CUSTOMER]",$rsProject[0]->customer,$html);
		$html = str_replace("[URL_CALENDAR_GOOGLE]",$rsProject[0]->urlCalendarGoogle,$html);
		$html = str_replace("[LIST_PRODUCT_OWER]",$selectUsersGroup1,$html);
		$html = str_replace("[LIST_SCRUM_MASTER]",$selectUsersGroup2,$html);
		$html = str_replace("[DATE_BEGIN]",$rsProject[0]->dateBegin,$html);
		$html = str_replace("[DATE_END_ESTIMED]",$rsProject[0]->dateEndEstimated,$html);
		$html = str_replace("[DESCRIPTION]",$rsProject[0]->description,$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível buscar o Projeto!");
		}

		return json_encode($return);
	}

	public static function newProject(){
		$return = null;

		$objUserGroupDAO = new UserGroupDAO(self::getDB());		
		$objUsersGroup1	 = $objUserGroupDAO->getUsersGroup(0,1);
		$objUsersGroup2	 = $objUserGroupDAO->getUsersGroup(0,2);
		
		$selectUsersGroup1 = "";
		if(count($selectUsersGroup1) > 0){
			foreach($objUsersGroup1 as $row)
				$selectUsersGroup1 .= '<option value="'.$row->idUser.'">'.$row->name.'</option>';
		}

		$selectUsersGroup2 = "";
		if(count($objUsersGroup2) > 0){
			foreach($objUsersGroup2 as $row)
				$selectUsersGroup2 .= '<option value="'.$row->idUser.'">'.$row->name.'</option>';
		}

		$html = file_get_contents(TEMPLATES."project.html");
		
		$html = str_replace("[TITLE]","Novo Projeto",$html);
		$html = str_replace("[ID_PROJECT]",0,$html);
		$html = str_replace("[NAME_PROJECT]","",$html);
		$html = str_replace("[CUSTOMER]","",$html);
		$html = str_replace("[URL_CALENDAR_GOOGLE]","",$html);
		$html = str_replace("[LIST_PRODUCT_OWER]",$selectUsersGroup1,$html);
		$html = str_replace("[LIST_SCRUM_MASTER]",$selectUsersGroup2,$html);
		$html = str_replace("[DATE_BEGIN]",date("d/m/Y"),$html);
		$html = str_replace("[DATE_END_ESTIMED]",date("d/m/Y"),$html);
		$html = str_replace("[DESCRIPTION]","",$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível criar o novo Projeto!");
		}

		return json_encode($return);
	}

	public static function saveProject($post){
		$return = null;
		$id		= 0;
		
		if(count($post) > 0){
			$objProjectDAO	= new ProjectDAO(self::getDB());		
			$objProject		= new Project();

			foreach($post as $key => $value){
				if(stristr($key,"date")){
					$tempDate = explode("/",$value);
					$value = $tempDate[2]."-".$tempDate[1]."-".$tempDate[0];
				}
				$objProject->$key = $value;
			}
			
			if($objProject->id > 0){		
				$objProjectDAO->save($objProject);
				$id = $objProject->id;
			}else{
				$id = $objProjectDAO->save($objProject);
			}
		}
		

		if($id > 0){
			$return =  array(true,"Projeto <i>\"". $objProject->name ."\"</i> salvo com sucesso.");
		}else{
			$return =  array(false,"Não foi possível salvar o Projeto!");
		}


		return json_encode($return);
	}

	public static function getProjects($post){

		$objProjectDAO	= new ProjectDAO(self::getDB());
		$objSprinttDAO	= new SprintDAO(self::getDB());
		$pagination		= (int)$post["page"];
		$archive		= (bool)$post["archive"];
		$search			= $post["s"];
		$status			= array(1,2);		

		if($archive)
			$status = array(3);

		if($_SESSION[USER]->idGroup == 1){
			$objProjects = $objProjectDAO->getProjects(0,$pagination,$status,$search);
		}else if($_SESSION[USER]->idGroup == 2){
			$objProjects = $objProjectDAO->getProjectsMaster(0,$pagination,$status,$_SESSION[USER]->id,$search);
		}else{
			$objProjects = $objProjectDAO->getProjectsNormalUser(0,$pagination,$status,$_SESSION[USER]->id,$search);
		}
		
		
		$numberRows		= $objProjectDAO->getNumberRows();		

		$html = '<table cellspacing="0" class="broom_table" summary="table">
						<thead>
						<tr>
							<th scope="col">Nome</th>
							<th scope="col">Cliente</th>
							<th scope="col">Sprint Atual</th>
							<th scope="col"><nobr>Data de Entrega do Sprint</nobr></th>
							<th scope="col"><nobr>Data de Entrega do Projeto</nobr></th>
							<th scope="col">Status</th>
							<th scope="col">Ações</th>
						</tr>
						</thead>
						<tbody>';
		
		if(count($objProjects) > 0){
			foreach($objProjects as $row){

				if($row->status < 3 && $_SESSION[USER]->idGroup == 1){
					$buttons = '<a class="icon delete" href="javascript:void(0);" onclick="deleteProject('.$row->id.')"><span>delete</span></a>
					<a class="icon edit" href="javascript:void(0);" onclick="editProject('.$row->id.')"><span>edit</span></a>
					'.($row->status == 2?'<a class="icon archive" href="javascript:void(0);" onclick="archiveProject('.$row->id.')"><span>arquivar</span></a>':'');
				}else{
					$buttons = '<a onclick="viewProject('.$row->id.')" href="javascript:void(0);" class="icon view"><span>view</span></a>';
				}
				
				$sprint = $objSprinttDAO->actualSprint($row->id);
				if($sprint->id != NULL){
					$orderSprint	= (int)$sprint->order;
					$dateEndSprint  = $sprint->dateEnd;
				}
				
				$html .= '<tr class="'.$class.'">
								<td><a href="'.SITE.'content/sprint/index.php?project='.$row->id.'">'.$row->name.'</a></td>
								<td>'.$row->customer.'</td>
								<td>'.$orderSprint.'</td>
								<td>'.$dateEndSprint.'</td>
								<td>'.$row->dateEndEstimated.'</td>
								<td>'.$objProjectDAO->statusNames[$row->status].'</td>
								<td>'.$buttons.'</td>
							</tr>';

				if($class == "odd")
					$class = "even";
				else
					$class = "odd";

			}
		}
										
		$html .= '</tbody></table>';		

		if(count($objProjects) > 0){
			$html .= Util::pagination($numberRows, $pagination);
			$return = array(true,$html);
		}else{
			$return = array(false, "<h2>Nenhum registro encontrado!</h2>");
		}	

		return json_encode($return);
	}

	public static function archiveProject($post){
		$id = (int)$post["id"];

		if($id > 0){
			$objProjectDAO = new ProjectDAO(self::getDB());
			$result = $objProjectDAO->archiveProject($id);
		}

		if($result){
			$return = array(true, "<h2>Projeto arquivado com sucesso.</h2>");
		}else{
			$return = array(false, "<h2>Não foi possível arquivar o projeto!</h2>");
		}

		return json_encode($return);
	}

	public static function deleteProject($post){
		$id = (int)$post["id"];

		if($id > 0){
			$objProjectDAO = new ProjectDAO(self::getDB());
			$result = $objProjectDAO->deleteProject($id);
		}

		if($result){
			$return = array(true, "<h2>Projeto excluído com sucesso.</h2>");
		}else{
			$return = array(false, "<h2>Não foi possível excluir o Projeto!</h2>");
		}

		return json_encode($return);
	}
}
?>